<?php
class BmAdminModule extends BaseModel{
    public $table = 'admin';
    public $_table = 'admin';
    public $_table_defaultmenu = 'admin_menu';

	public function __construct($db_choose = 'default'){
		parent::__construct($db_choose);
	}

    /**
     * 获取管理员列表
     *
     * @param mixed $start
     * @param mixed $count
     */
    public function getAllUser($start, $count, $isEnable = ""){
        $where = "1=1 ";
        if ($isEnable) {
            $where .= " and isEnabled = '{$isEnable}'";
        }
        $sql = "select * from {$this->_table} where {$where} LIMIT :start, :count";
        $this->_db->prepare($sql);
        $this->_db->bindValue(":start", $start, PDO::PARAM_INT);
        $this->_db->bindValue(":count", $count, PDO::PARAM_INT);

        if($this->_db->execute()){
            return $this->_db->fetchAll(PDO::FETCH_ASSOC);
        }

        return null;
    }

    public function getUserInfoByUserIDS($userid, $fileds = ''){
        if(empty($userid)){
            return false;
        }

        if(empty($fileds)){
            $fileds = 'adminid,name,account';
        }

        $sql = "select {$fileds} from {$this->_table} where adminid in (".$userid.")";
        $this->_db->prepare($sql);
        if($this->_db->execute()){
            return $this->_db->fetchAll();
        }
        return null;
    }

    public function getUserInfoByPhone($phone, $fileds = '*'){
        if(empty($phone)){
            return false;
        }

        $sql = "select {$fileds} from {$this->_table} where phone='{$phone}'";
        $this->_db->prepare($sql);
        if($this->_db->execute()){
            return $this->_db->fetch();
        }
        return null;
    }

    public function getUserInfoByAdminid($adminid, $fileds = '*'){
        if(empty($adminid)){
            return false;
        }

        $sql = "select {$fileds} from {$this->_table} where adminid={$adminid}";
        $this->_db->prepare($sql);
        if($this->_db->execute()){
            return $this->_db->fetch();
        }
        return null;
    }

    /**
     * 修改管理员角色
     *
     * @param mixed $userid
     * @param mixed $role
     */
    public function editAdminRole($userid, $role){
        if(! AppCommon::intParse($userid, $adminid) || $adminid <= 0){
            return false;
        }

        $sql = "update {$this->_table} set `defaultrole`=:defaultrole where adminid=:adminid";
        $this->_db->prepare($sql);

        $this->_db->bindValue(":defaultrole", $role, PDO::PARAM_STR);
        $this->_db->bindValue(":adminid", $adminid, PDO::PARAM_INT);
        if($this->_db->execute()){
            return $this->_db->rowCount();
        }

        return false;
    }

    /**
     * 根据key查询对应动作信息
     *
     * @param string $action_key
     * @return void mixed NULL
     */
    public function getMenuByKey($action_key){
        if(empty($action_key)){
            return;
        }

        $this->_db->prepare("select menuid,type, name,action_key,action,parentid,isshow from {$this->_table_defaultmenu} where action_key=:action_key limit 0,1 ");
        $this->_db->bindValue(":action_key", $action_key, PDO::PARAM_STR);
        if($this->_db->execute()){
            return $this->_db->fetch(PDO::FETCH_ASSOC);
        }

        return null;
    }
}
